package cn.jimmie.learn.viewArt;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.jimmie.learn.viewArt.refreshview.RefreshLayout;
import cn.jimmie.learn.viewArt.refreshview.core.RefreshState;


/**
 * function :
 *
 * <p> Create Time:  2019年04月13日 13:50 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class ListRefreshViewActivity extends AppCompatActivity {
    RefreshLayout layout;
    ListView listView;
    TextView emptyView;
    String[] itemsTmp =
            {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};
//            {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
    List<String> items = new ArrayList<>(Arrays.asList(itemsTmp));
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_refresh_activity);

        layout = findViewById(R.id.refresh_layout);

        listView = findViewById(R.id.list_view);
        emptyView = findViewById(R.id.empty_view);

        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
        listView.setAdapter(adapter);

        layout.setHeadViewEnable(true);
        layout.setFootViewEnable(true);
        layout.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onPullRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListRefreshViewActivity.this, "刷新结束", Toast.LENGTH_SHORT).show();
                        items.add(itemsTmp[count % 10]);
                        items.add(itemsTmp[count % 10]);
                        adapter.notifyDataSetChanged();
                        layout.stopPullRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListRefreshViewActivity.this, "刷新结束", Toast.LENGTH_SHORT).show();

                        if (count > 2) {
                            layout.stopLoadMore(RefreshState.ResultState.END);
                            return;
                        }
                        count++;
                        items.add(itemsTmp[count % 10]);
                        items.add(itemsTmp[count % 10]);
                        adapter.notifyDataSetChanged();
                        layout.stopLoadMore(RefreshState.ResultState.FAILURE);
                    }
                }, 2000);
            }
        });
    }

    int count = 0;

    boolean tag = false;

    public void onBottomClick(View view) {
        int listViewVisible = tag ? View.VISIBLE : View.GONE;
        int emptyViewVisible = !tag ? View.VISIBLE : View.GONE;
        listView.setVisibility(listViewVisible);
        emptyView.setVisibility(emptyViewVisible);
        tag = !tag;
    }
}
